package com.baizhi.controller;


import com.baizhi.entity.User;
import com.baizhi.service.IUserService;
import com.baizhi.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author longyang
 * @since 2022-01-11
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;
    @GetMapping("/login")
    public Map<String,String> selectUser(User user){
        System.err.println(user);
        User newuser = userService.selectUser(user);
        Map<String,String> resultMap=new HashMap<>();
        if (newuser!=null){
            //token里面要存储的数据
            Map<String,String> map = new HashMap();
            map.put("username",user.getUsername());
            //生成token
            String token = JwtUtils.getToken(map);
            System.err.println(token);
            //往resultMap集合里存值返回
            resultMap.put("token",token);
            resultMap.put("username",user.getUsername());
            resultMap.put("msg","登录成功");
        }else {
            resultMap.put("msg","登录失败");
        }
        return  resultMap;
    }
}

